package com.hujing.springsource.a14;

import org.springframework.cglib.proxy.MethodInterceptor;

import java.lang.reflect.Method;

/**
 * cglib代理
 */
public class Proxy extends Target{
    
    private MethodInterceptor interceptor;
    
    public Proxy(MethodInterceptor interceptor){
        this.interceptor = interceptor;
    }
    
    //定义目标方法
    static Method save0;
    static Method save1;
    static Method save2;
    
    static{
        try {
            save0 = Target.class.getDeclaredMethod("save");
            save1 = Target.class.getDeclaredMethod("save", int.class);
            save2 = Target.class.getDeclaredMethod("save", long.class);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void save() {
        try {
            interceptor.intercept(this,save0,new Object[0],null);
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void save(int i) {
        try {
            interceptor.intercept(this,save1,new Object[]{i},null);
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void save(long j) {
        try {
            interceptor.intercept(this,save2,new Object[]{j},null);
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }
}
